import * as fgui from "fairygui-cc";
import AbstractUIWindow from "../../window/AbstractUIWindow";
import UICore from "../../../../Core/Res/UICore";
import { Grid } from "./component/Grid";
import { ModelGameRobot } from "../../model/ModelGameRobot";
import { Plane } from "./component/Plane";
import { math } from "cc";
import EventMgr from "../../../../Core/Event/EventMgr";
import { ViewManager } from "../../base/ViewManager";
import { LocalUtil } from "../../../../Core/Utils/LocalUtil";
import ProtoBufManager from "../../../proto/ProtoBufManager";
import polemo from "../../../polemo/polemo";
import { GameModels } from "../../socket/GameModels";
import { LocalizationManager } from "../../../../Core/Language/LocalizationManager";
import Log from "../../../../Core/Log/Log";
export class FormationWindow extends AbstractUIWindow {
    private board: fgui.GComponent;
    private plane1: Plane;
    private plane2: Plane;
    private plane3: Plane;
    private planeList: Array<Plane>;
    private save: fgui.GButton;
    private randombtn: fgui.GButton;
    private isoverlap: boolean = false;
    protected getResList(): Array<string> {
        return [`Plane`];
    }
    protected onInit(): void {
        this._view = UICore.createObject("Plane", "FormationWindow").asCom;
    }
    protected onShow(): void {
        EventMgr.on(`planeTest`, this.planeTest, this);
        this.plane1 = this.board.getChild("plane1") as Plane;
        this.plane2 = this.board.getChild("plane2") as Plane;
        this.plane3 = this.board.getChild("plane3") as Plane;
        this.planeList = [this.plane1, this.plane2, this.plane3];
        const pl = LocalUtil.ins.getCookie(`plane1`);
        if (pl) {
            Log.ins.log(1, `本地存储的编队`, pl);
            this.pullplane(pl);
        } else {
            this.randomplane();
        }
        this.save.onClick(this.clicksave, this);
        this.randombtn.onClick(this.randomplane, this);
    }
    private randomplane(): void {
        const str = ModelGameRobot.randomthreeplane();
        this.pullplane(str);
    }
    private pullplane(str: string): void {
        if (!str) {
            return;
        }
        const pla = str.split(`|`);
        for (let i = 0; i < pla.length; i++) {
            const pos = pla[i].split(`+`)[0];
            const ratio = pla[i].split(`+`)[1];
            const x = Number(pos.split(`-`)[0])
            const y = Number(pos.split(`-`)[1])
            this.planeList[i].x = ModelGameRobot.mesh()[x][y][`x`];
            this.planeList[i].y = ModelGameRobot.mesh()[x][y][`y`];
            this.planeList[i].setposTarget(this.board.x, this.board.y, this.view.x, this.view.y, this.board.width, this.board.height);
            this.planeList[i].state = Number(ratio);
        }
    }
    private clicksave(): void {
        if (this.isoverlap) {
            ViewManager.ins.showtips("不能摆放重叠的飞机");
            return;
        }
        LocalUtil.ins.setCookie(`plane1`, `${this.plane1.fristgrid}+${this.plane1.state}|${this.plane2.fristgrid}+${this.plane2.state}|${this.plane3.fristgrid}+${this.plane3.state}`);

        ViewManager.ins.showtips("保存成功");
    }
    /**
     * 重叠检测
     */
    private planeTest(): void {
        const result = ModelGameRobot.findDuplicates(this.plane1.gridArray, this.plane2.gridArray, this.plane3.gridArray);
        this.plane1.overlap(result);
        this.plane2.overlap(result);
        this.plane3.overlap(result);
        this.isoverlap = result.length > 0;
    }

}